package cn.caplike.demo.repository.fastjson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

/**
 * Description:
 * https://blog.csdn.net/qq_33589510/article/details/108815755?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-108815755-blog-102914545.pc_relevant_paycolumn_v3&spm=1001.2101.3001.4242.2&utm_relevant_index=4
 * @author LiKe
 * @version 1.0.0
 * @date 2022-05-26 16:14
 */
@SuppressWarnings("ALL")
public class TypeReference {

    public static void main(String[] args) {
        IntMap intMap = new IntMap();

        System.out.println(intMap.getClass().getSuperclass());

        Type type = intMap.getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType p = (ParameterizedType) type;
            for (Type t : p.getActualTypeArguments()) {
                System.out.println(t);
            }
        }

        System.out.println("=====newclass=====");
        HashMap<String, Integer> newIntMap = new HashMap<>();

        System.out.println(newIntMap.getClass().getSuperclass());

        Type newClassType = newIntMap.getClass().getGenericSuperclass();
        if (newClassType instanceof ParameterizedType) {
            ParameterizedType p = (ParameterizedType) newClassType;
            for (Type t : p.getActualTypeArguments()) {
                System.out.println(t);
            }
        }

        System.out.println("=====subclass=====");
        HashMap<String, Integer> subIntMap = new HashMap<String, Integer>() {
        };

        System.out.println(subIntMap.getClass().getSuperclass());

        Type subClassType = subIntMap.getClass().getGenericSuperclass();
        if (subClassType instanceof ParameterizedType) {
            ParameterizedType p = (ParameterizedType) subClassType;
            for (Type t : p.getActualTypeArguments()) {
                System.out.println(t);
            }
        }
    }


    public static class IntMap extends HashMap<String, Integer> {
    }

}
